using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TreeView.Logic;
using System.Collections;
using TreeView.Graphic;
using Data.DataTranslator;

namespace WindowsApplication2.TreeView.Graphic
{
    public partial class pnl_TreeResult : UserControl
    {
        #region Members

        private RadialTree dispTree;
        private RadialTree originalTree;
        private ArrayList guiTreeNodes;
        private Brush selectedNodeColor;

        #endregion

        public pnl_TreeResult()
        {
            InitializeComponent();
            selectedNodeColor = Brushes.White;
        }

        public void EnableResultView(string spreadAngle, RadialTree treeData)
        {
            this.panel1.Visible = true;
            this.rpnl_Control.Visible = true;
            this.rpnl_Distribution.Visible = true;
            this.rlbl_TreeResErr.Visible = false;
            int angle = Int32.Parse(spreadAngle);
            originalTree = treeData;
            originalTree.createGUIRadTree(originalTree.TreeNodes, panel1.Width, panel1.Height);
            dispTree = originalTree;
            ApplyTreeChange();
            //panel1.Refresh();
 
            this.rcmbx_PaintType.SelectedIndex = 0;
            this.rlbl_TreeAngleValue.Text = spreadAngle;
            this.rhsb_Angle.Value = (int)(dispTree.Teta * 180);
        }

        public void printTree(PaintEventArgs e)
        {
            foreach (RadNodeGUI node in guiTreeNodes)
            {
                node.paintNode(panel1);
            }
            foreach (RadNodeGUI node in guiTreeNodes)
            {
                node.paintBody(panel1);
            }
        }

        private RadNodeGUI findContain(int x, int y)
        {
            try
            {
                RadNodeGUI FounNode = null;
                foreach (RadNodeGUI node in guiTreeNodes)
                {
                    if (node.NodeBody.Contains(x, y))
                        FounNode = node;
                    if (node.NodeBrush == selectedNodeColor)
                    {
                        node.resetBrush();
                        node.paintBody(panel1);
                    }
                }
                return FounNode;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("The tree is empty", "Error!!!");
                return null;
            }
        }

        private void onMousMove(RadNodeGUI node, MouseEventArgs e)
        {
            if (node != null)
            {
                Cursor.Current = Cursors.Hand;
                node.NodeBrush = selectedNodeColor;
                node.paintBody(panel1);
                if (node.LogicNode.isLeaf())
                {
                    ttip_Name.SetToolTip(panel1, node.LogicNode.Name);
                    PieProperties.Visible = false;
                }
                else
                {
                    ttip_TreeHelp.SetToolTip(panel1, "click me to see my sub tree");
                    //here need to change to the porperties names from moran.
                    ArrayList[] clusterDistribution = new ArrayList[2];
                    clusterDistribution = kDimMap.getPropertiesDivisionOfCluster(node.LogicNode.SonsCodeNames, (int)rnud_DistributionFeature.Value - 1);
                    ArrayList propNames = (ArrayList)clusterDistribution[1];

                    //here we need to get the amount of genome in each of the properties
                    ArrayList propValue = (ArrayList)clusterDistribution[0];


                    //Series initialization
                    PieProperties.Series.Clear();
                    Dundas.Charting.WinControl.Series clusterSeries = new Dundas.Charting.WinControl.Series();
                    //clusterSeries.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
                    clusterSeries.ChartType = "Pie";
                    clusterSeries.CustomAttributes = "PieLabelStyle=Inside";
                    clusterSeries.Name = "clusterDistribution";
                    clusterSeries.ShadowOffset = 2;
                    clusterSeries.ShowLabelAsValue = true;
                    clusterSeries.ShowInLegend = true;

                    for (int i = 0; i < propValue.Count; i++)
                    {
                        double[] data = new double[1];
                        data[0] = (double)propValue[i];
                        Dundas.Charting.WinControl.DataPoint PropValue = new Dundas.Charting.WinControl.DataPoint();
                        PropValue.LegendText = (string)propNames[i];//this shuld be shorter name
                        PropValue.LegendText = PropValue.LegendText.Substring(0, 4);
                        PropValue.LegendToolTip = (string)propNames[i];
                        PropValue.XValue = (double)i;
                        PropValue.YValues = data;
                        clusterSeries.Points.Add(PropValue);
                    }
                    PieProperties.Series.Add(clusterSeries);
                    PieProperties.Visible = true;
                }
            }
        }

        private void onMousClick(double newTeta, RadNodeGUI foundNode, RadialTree DispTree, MouseEventArgs e)
        {
            if (foundNode != null)
            {
                dispTree = dispTree.SubRadialTree(foundNode.LogicNode, newTeta);
                dispTree.createGUIRadTree(dispTree.TreeNodes, panel1.Width, panel1.Height);
                ApplyTreeChange();
                panel1.Refresh();
            }
        }

        private void ApplyTreeChange()
        {
            if (guiTreeNodes == null)
                guiTreeNodes = new ArrayList();
            else
                guiTreeNodes.Clear();
            foreach (RadialTreeNode node in dispTree.TreeNodes)
                guiTreeNodes.Add(node.NodeGUI);
        }

        public void makeColorTree(int property)
        {
            dispTree.createGUIRadTree(dispTree.TreeNodes, panel1.Width, panel1.Height, property);
            ApplyTreeChange();
            panel1.Refresh();
        }

        private void rpnl_TreeResult_Paint(object sender, PaintEventArgs e)
        {
            if ((dispTree != null) && (dispTree.TreeNodes.Count != 0))
            {
                Graphics treeShowGraphics = e.Graphics;
                treeShowGraphics.FillEllipse(Brushes.Black, 0, 0, panel1.Width, panel1.Height);
                //if we don't have nodes ready to print
                if (guiTreeNodes == null)
                {
                    dispTree.createGUIRadTree(originalTree.TreeNodes, panel1.Width, panel1.Height);
                    ApplyTreeChange();
                }
                //go over the tree and print all the nodes
                printTree(e);
            }
        }

        private void rbtn_TreeReset_Click(object sender, EventArgs e)
        {
            if (dispTree != null)
            {
                rlbl_TreeAngleValue.Text = ((int)(originalTree.Teta * 180)).ToString();
                rhsb_Angle.Value = ((int)(originalTree.Teta * 180));
                dispTree = originalTree;
                dispTree.createGUIRadTree(originalTree.TreeNodes, panel1.Width, panel1.Height);
                if (rcmbx_PaintType.SelectedIndex == 0)
                {
                    rnud_ColorFeature.Visible = false;
                    rlbl_ColorFeatureSelection.Visible = false;
                    makeColorTree(-1);
                }
                if (rcmbx_PaintType.SelectedIndex == 1)
                {
                    rnud_ColorFeature.Visible = true;
                    rlbl_ColorFeatureSelection.Visible = true;
                    makeColorTree((int)rnud_ColorFeature.Value);
                }
                ApplyTreeChange();
                panel1.Refresh();
            }
        }

        private void rcmbx_PaintType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rcmbx_PaintType.SelectedIndex == 0)
            {
                rnud_ColorFeature.Visible = false;
                rlbl_ColorFeatureSelection.Visible = false;
                makeColorTree(-1);
            }
            if (rcmbx_PaintType.SelectedIndex == 1)
            {
                rnud_ColorFeature.Visible = true;
                rlbl_ColorFeatureSelection.Visible = true;
                makeColorTree((int)rnud_ColorFeature.Value);
            }
        }

        private void rnud_ColorFeature_ValueChanged(object sender, EventArgs e)
        {
            makeColorTree((int)rnud_ColorFeature.Value);
        }

        private void rhsb_Angle_MouseCaptureChanged(object sender, EventArgs e)
        {
            if ((dispTree != null) && (rhsb_Angle.Capture == false))
            {
                double spreadAngle = (Double.Parse(rlbl_TreeAngleValue.Text)) / 180;
                if (spreadAngle > 2)
                    spreadAngle = 2;
                dispTree = dispTree.SubRadialTree(null, spreadAngle);
                dispTree.createGUIRadTree(dispTree.TreeNodes, panel1.Width, panel1.Height);
                if (rcmbx_PaintType.SelectedIndex == 0)
                {
                    rnud_ColorFeature.Visible = false;
                    rlbl_ColorFeatureSelection.Visible = false;
                    makeColorTree(-1);
                }
                if (rcmbx_PaintType.SelectedIndex == 1)
                {
                    rnud_ColorFeature.Visible = true;
                    rlbl_ColorFeatureSelection.Visible = true;
                    makeColorTree((int)rnud_ColorFeature.Value);
                }
                ApplyTreeChange();
                panel1.Refresh();
            }
        }

        private void rbtn_SaveNewick_Click(object sender, EventArgs e)
        {
            string sNewick = ((RadialTreeNode)originalTree.TreeNodes[originalTree.TreeNodes.Count - 1]).ToString();
        }

        private void rhsb_Angle_Scroll(object sender, ScrollEventArgs e)
        {
            rlbl_TreeAngleValue.Text = rhsb_Angle.Value.ToString();
        }

        private void rbtn_SaveResults_Click(object sender, EventArgs e)
        {
            ArrayList t = originalTree.RadTree2MergList();
        }

        private void rpnl_TreeResult_MouseClick(object sender, MouseEventArgs e)
        {
            if (dispTree != null)
            {
                double spreadAngle = (Double.Parse(rlbl_TreeAngleValue.Text)) / 180;
                if (spreadAngle > 2)
                    spreadAngle = 2;
                RadNodeGUI foundNode = findContain(e.X, e.Y);
                if (foundNode != null)
                    onMousClick(spreadAngle, foundNode, dispTree, e);
            }
        }

        private void rpnl_TreeResult_MouseMove(object sender, MouseEventArgs e)
        {
            if (dispTree != null)
            {
                RadNodeGUI foundNode = findContain(e.X, e.Y);
                if (foundNode != null)
                    onMousMove(foundNode, e);
            }
        }


     
    }
}
